package com.ydd.service.impl;

import com.alibaba.fastjson.JSON;
import com.ydd.constant.ConfigConstant;
import com.ydd.constant.MessageConstant;
import com.ydd.constant.SysDictConstant;
import com.ydd.context.BaseContext;
import com.ydd.dto.IndexSetTopPage;
import com.ydd.dto.LoginDTO;
import com.ydd.entity.SysConfig;
import com.ydd.entity.SysDict;
import com.ydd.entity.SysRole;
import com.ydd.entity.SysUser;
import com.ydd.mapper.*;
import com.ydd.result.Result;
import com.ydd.service.IndexService;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    SysConfigMapperMB configMapper;
    @Autowired
    private SysUserMapperMB userMapper;

    @Autowired
    private SysRoleMapperMB roleMapper;
    @Autowired
    private SysMenuMapperMB menuMapper;
    @Autowired
    private SysDictMapperMB dictMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 获取系统配置接口
     *
     * @return
     */
    @Override
    public Result getConfig() {
        SysConfig Config = new SysConfig();
        Config.setTabValue(ConfigConstant.TAB_VALUE_SYSTEM);
        List<SysConfig> configList = configMapper.getConfig(Config);
        GetConfigVO vo = new GetConfigVO();
        if (configList.size() > 0 && configList != null) {
            for (SysConfig sysConfig : configList) {
                if (sysConfig.getAttrKey().equals("login_bg")) {
                    vo.setLogin_bg(sysConfig.getAttrValue());
                }
                if (sysConfig.getAttrKey().equals("login_title")) {
                    vo.setLogin_title(sysConfig.getAttrValue());
                }
                if (sysConfig.getAttrKey().equals("system_name")) {
                    vo.setSystem_name(sysConfig.getAttrValue());
                }
                if (sysConfig.getAttrKey().equals("system_logo")) {
                    vo.setSystem_logo(sysConfig.getAttrValue());
                }
                if (sysConfig.getAttrKey().equals("web_ico")) {
                    vo.setWeb_ico(sysConfig.getAttrValue());
                }

            }
        }
        return Result.success(vo);
    }

    /**
     * 登录相关接口
     *
     * @param dto
     * @return
     */
    @Override
    public Result login(LoginDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.PASSWORD_ERROR);
        }
        System.out.println(dto.getPassword());
        dto.setPassword(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()));
        SysUser user = userMapper.getUserPasswordAndName(dto);
        if (user == null) {
            return Result.error(MessageConstant.PASSWORD_ERROR);
        }
        if (user.getStatus() == 0) {
            return Result.error(MessageConstant.ACCOUNT_LOCKED);

        }
        user.setLoginDate(LocalDateTime.now());
        sysUserMapper.updateById(user);
        String secretKey = JwtUtil.getToken(user.getId());
        HashMap<Object, Object> map = new HashMap<>();
        map.put("token", secretKey);
        return Result.success(map);
    }

    /**
     * 获取用户资料
     *
     * @return
     */
    @Override
    public Result profile() {
        Long id = BaseContext.getCurrentId();
        id = 1L;
        SysUser user = userMapper.getUserById(id);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        ProfileV0 profileV0 = new ProfileV0();
        List<String> roleIdList = null;
        if (user.getRoleId() != null) {
            //取出角色id集合
            roleIdList = JSON.parseArray(user.getRoleId(), String.class);
            //根据id列表取出角色集合
            List<SysRole> role = roleMapper.getByIds(roleIdList);
            //获取角色名称集合
            List<String> list = role.stream()
                    .map(SysRole::getName)
                    .collect(Collectors.toList());
            userInfo.setRole_id(roleIdList);
            //设置角色名称
            userInfo.setRolename(list);
            //用户菜单权限
            List<MenuList> menuLists = menuMapper.getByRoleIdlist(roleIdList);
            List<SysDict> dictList = dictMapper.getBy();
            Map<String, String> interfaceType = new HashMap<>();
            Map<String, String> orderType = new HashMap<>();
            Map<String, String> reportType = new HashMap<>();
            Map<String, String> tableType = new HashMap<>();
            Map<String, String> theme = new HashMap<>();
            for (SysDict sysDict : dictList) {
                if (sysDict.getType().equals(SysDictConstant.INTERFACE_TYPE)) {
                    interfaceType.put(sysDict.getValue(), sysDict.getLabel());
                }
                if (sysDict.getType().equals(SysDictConstant.ORDER_TYPE)) {
                    orderType.put(sysDict.getValue(), sysDict.getLabel());
                }
                if (sysDict.getType().equals(SysDictConstant.REPORT_TYPE)) {
                    reportType.put(sysDict.getValue(), sysDict.getLabel());
                }
                if (sysDict.getType().equals(SysDictConstant.TABLE_TYPE)) {
                    tableType.put(sysDict.getValue(), sysDict.getLabel());
                }
                if (sysDict.getType().equals(SysDictConstant.THEME)) {
                    theme.put(sysDict.getValue(), sysDict.getLabel());
                }
            }
            DictsList dictsList = DictsList.builder()
                    .interfaceType(interfaceType)
                    .orderType(orderType)
                    .tableType(tableType)
                    .theme(theme)
                    .reportType(reportType)
                    .build();

            List<String> btn = new ArrayList<>();
            btn.add("*");
            profileV0.setUserInfo(userInfo);
            profileV0.setBtnList(btn);
            profileV0.setMenuList(menuLists);
            profileV0.setDictsList(dictsList);

        }


        return Result.success(profileV0);
    }

}
